package com.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class ConfigParam {

	/** 是否记录更新日志 */
	private static boolean log = true;
	/** 配置文件路径 */
//	private static String path = "resource/properties.properties";
	private static String path = "properties.properties";
	private static Properties properties = new Properties();
	private static List<String> paramList = new ArrayList<String>();

	static{

		paramList.add("IP");
		paramList.add("DBNAME");
		paramList.add("NAME");
		paramList.add("PASSW");
		paramList.add("HANDLEDATA");

		FileInputStream in = null ;
		File configFile = null;
		try {
			configFile = new File(path);
			if( !configFile.exists()){

				configFile.createNewFile();
			}

			in = new FileInputStream(configFile);
			properties.load(in);

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			// 加载配置文件后 关闭流资源
			if(null != in ){
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 *  设置配置文件参数
	 * @param key
	 * @param val
	 */
	public static void set(String key, String val){

		properties.setProperty(key, val);
		fileSync("update ["+ key + "] to [" + val + "];");

	}

	/**
	 * 配置 和 文件同步
	 * @param detail
	 */
	private static void fileSync(String detail){

		FileOutputStream oFile = null;
		try {

			oFile = new FileOutputStream(path);

			if(log){

				properties.store(oFile, detail);
			}else{
				properties.store(oFile, "");
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(null != oFile){

				try {
					oFile.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 *  获取配置参数
	 * @param key
	 * @return
	 */
	public static String get(String key) {

		return properties.containsKey(key)? properties.getProperty(key) : "";
	}

	/**
	 * 登陆 更新各项参数值
	 * @param params
	 */
	public static void refreshParam(Map<String, String> params){

		boolean sysc = false;
		StringBuilder details = new StringBuilder();
		Set<String> keys = params.keySet();
		for(String key : keys){
			if(paramList.contains(key) && !params.get(key).equals(properties.get(key))){
				properties.setProperty(key, params.get(key));
				sysc = true;
				details.append("update ["+ key + "] to [" + params.get(key) + "];");
			}
		}
		if(sysc){
			fileSync(details.toString());
		}
	}

	/**
	 * 当前版本是可修改数据
	 *
	 * @return
	 */
	public static boolean handleData(){
		if( properties.containsKey("HANDLEDATA")
				&& "true".equals(properties.get("HANDLEDATA").toString().toLowerCase())){
			return true;
		}else{
			return false;
		}
	}

}